
package com.wyzz.hr.bo.impl;

import java.util.*;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IIodefBo;
import com.wyzz.hr.dao.impl.IodefDAOImpl;
import com.wyzz.hr.pojo.Iodef;
import com.wyzz.hr.pojo.Iomatch;
import com.wyzz.hr.util.Pager;


public class IodefBoImpl implements IIodefBo {

	public IodefBoImpl() {
		iodefdao = null;
	}

	public Iodef loadObject(String id) {
		return loadObject(id, null);
	}

	public Iodef loadObjectByName(String name) {
		return (Iodef) iodefdao.loadObjectByName(com.wyzz.hr.pojo.Iodef.class,
				Iodef.PROP_IO_NAME, name, null, new boolean[0]);
	}

	public Iodef loadObject(String id, String fetch[]) {
		return (Iodef) iodefdao.loadObject(com.wyzz.hr.pojo.Iodef.class, id,
				fetch, new boolean[0]);
	}

	public boolean saveObject(Iodef obj) {
		iodefdao.saveObject(obj);
		return true;
	}

	public boolean updateObject(Iodef obj) {
		iodefdao.updateObject(obj);
		return true;
	}

	public boolean deleteObject(String id) {
		iodefdao.deleteObject(com.wyzz.hr.pojo.Iodef.class, id);
		return true;
	}

	public List searchIodef(Iodef iodef, Pager pager) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Iodef.class);
		checkOrderMethod(detachedCriteria, pager.getOrder());
		if (iodef == null)
			return iodefdao.findByCriteria(detachedCriteria);
		if (iodef.getIoName() != null)
			detachedCriteria.add(Restrictions.like("ioName",
					(new StringBuilder()).append("%").append(iodef.getIoName())
							.append("%").toString()));
		if (iodef.getIoType() != null)
			detachedCriteria.add(Restrictions.eq("ioType", iodef.getIoType()));
		if (iodef.getIoMatchType() != null)
			detachedCriteria.add(Restrictions.eq("ioMatchType", iodef
					.getIoMatchType()));
		if (iodef.getIoModule() != null)
			detachedCriteria.add(Restrictions.eq("ioModule", iodef
					.getIoModule()));
		return iodefdao.findByCriteria(detachedCriteria);
	}

	public String insertIodef(Iodef iodef) {
		if (iodef != null) {
			iodef.setIoId(UUID.randomUUID().toString());
			iodefdao.saveObject(iodef);
		}
		return iodef.getIoId();
	}

	public String deletIodef(String iodefId) {
		String hql = (new StringBuilder()).append(
				"delete from Iomatch where iodef.ioId ='").append(iodefId)
				.append("'").toString();
		iodefdao.exeHql(hql);
		hql = (new StringBuilder()).append("delete from Iodef where ioId='")
				.append(iodefId).append("'").toString();
		iodefdao.exeHql(hql);
		return iodefId;
	}

	public Iodef searchOneIodef(String iodefId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Iodef.class);
		detachedCriteria.setFetchMode("iomatchs", FetchMode.JOIN);
		detachedCriteria.add(Restrictions.eq("ioId", iodefId));
		List result = iodefdao.findByCriteria(detachedCriteria);
		if (result != null && result.size() != 0)
			return (Iodef) result.get(0);
		else
			return null;
	}

	public Iodef searchIodefByName(String iodefName) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Iodef.class);
		detachedCriteria.add(Restrictions.eq("ioName", iodefName));
		List result = iodefdao.findByCriteria(detachedCriteria);
		if (result != null && result.size() != 0)
			return (Iodef) result.get(0);
		else
			return null;
	}

	public String updateIodef(Iodef iodef) {
		Iodef iodefOld = searchOneIodef(iodef.getIoId());
		iodef.setIoCreateBy(iodefOld.getIoCreateBy());
		iodef.setIoCreateTime(iodefOld.getIoCreateTime());
		iodef.setIoIsExtend(iodefOld.getIoIsExtend());
		Set matchs = iodef.getIomatchs();
		String matchIds = "";
		for (Iterator i$ = matchs.iterator(); i$.hasNext();) {
			Iomatch iomatch = (Iomatch) i$.next();
			matchIds = (new StringBuilder()).append(
					"".equals(matchIds) ? matchIds : (new StringBuilder())
							.append(matchIds).append("',").toString()).append(
					"'").append(iomatch.getIomatchId()).toString();
		}

		matchIds = (new StringBuilder()).append(matchIds).append("'")
				.toString();
		String hql = (new StringBuilder()).append(
				"delete from Iomatch where iodef.ioId='").append(
				iodef.getIoId()).append("' and iomatchId not in (").append(
				matchIds).append(")").toString();
		iodefdao.exeHql(hql);
		iodefdao.updateObject(iodef);
		return iodef.getIoId();
	}

	public IodefDAOImpl getIodefdao() {
		return iodefdao;
	}

	public void setIodefdao(IodefDAOImpl iodefdao) {
		this.iodefdao = iodefdao;
	}

	private void checkOrderMethod(DetachedCriteria detachedCriteria,
			String order) {
		if (order != null && order.trim().length() > 1
				&& order.indexOf('-') != -1) {
			String pram[] = order.split("-");
			if (pram[0] == null || pram[0].length() < 1)
				return;
			String fetch[] = pram[0].split("\\.");
			String orde = pram[0];
			if (pram.length == 2 && pram[1].equalsIgnoreCase("up"))
				detachedCriteria.addOrder(Order.asc(orde));
			else
				detachedCriteria.addOrder(Order.desc(orde));
		}
	}

	public boolean updateIodef(Iodef iodef, List ombList, List omList) {
		iodefdao.updateObject(iodef);
		iodefdao.saveOrupdate(ombList);
		iodefdao.saveOrupdate(omList);
		return true;
	}

	private IodefDAOImpl iodefdao;
}
